(function QSearch(window){

	var 
		QSearch = {},
		List = [];
		
	window.QSearch = QSearch;
	
	/**
	 * 工具对象
	 */
	QSearch.Util = {};

	function search(_url, _type){
		
		var url = _url,
			type = _type || "json",
			conditions = [],
			conditionNames = {},
			pageNum,
			pageSize,
			callback_Fn = function(){};
		
		//不提供url更改方式
		this.getUrl = function(){return url;}
		
		this.getType = function(){return type;}
		
		this.setType = function(_type){type = _type;}
		
		this.getPageNum = function(){return pageNum;}
		
		this.setPageNum = function(_num){pageNum = _num;}
		
		this.setPageSize = function(_size){pageSize = _size;}
		
		this.getPageSize = function(){return pageSize;}
		
		this.getCondition = function(_name){
			
			if(_name){
				return conditions[conditionNames[_name]];
			} else {
				return conditions;
			}
		}
		
		this.clear = function(){
			pageNum = 0,
			pageSize = 0,
			conditionNames = {},
			conditions = [];
		}
		
		this.addCondition = function(_name, _value, _pattern, _order, _orderSeq){
			
			if(conditionNames[_name]){
				var tmp = conditions[conditionNames[_name]];
			} else {
				var tmp = new condition(_name, _value, _pattern, _order, _orderSeq);
				conditionNames[_name] = conditions.length;
				conditions[conditions.length] = tmp;
			}
			
			return tmp;
		}
		
		this.serialize = function(){
			
			var tmpList = [];
			if(pageNum != undefined){
				tmpList.push("pageNum=" + pageNum);
			}
			
			if(pageSize != undefined){
				tmpList.push("pageSize=" + pageSize);
			}
			
			for(var i=0, len=conditions.length; i<len; i+=1){
			
				var enable = false;
				tmpList.push("condition[" + i + "].name=" + conditions[i].getName());
				if(conditions[i].getValue() != undefined){
					tmpList.push("condition[" + i + "].value=" + conditions[i].getValue());
					
					if(conditions[i].getPattern() != undefined){
						tmpList.push("condition[" + i + "].pattern=" + conditions[i].getPattern());
					} else {
						tmpList.push("condition[" + i + "].pattern=LIKE");
					}
					enable = true;
				}
				
				if(conditions[i].getOrder() != undefined){
					tmpList.push("condition[" + i + "].order=" + conditions[i].getOrder());
					if(conditions[i].getOrderSeq()){
						tmpList.push("condition[" + i + "].orderSeq=" + conditions[i].getOrderSeq());
					}
					enable = true;
				}
				
				if(enable == false){
					tmpList.pop();
				}
			}
			
			return tmpList.join("&");
		}
		
		this.doSearch = function(){
			
			var caller = this,
				$post = $.post(url, this.serialize(), function(_result){
					callback_Fn.call(caller, _result);
				}, type);
			/*设置默认错误方法*/
			$post.error(forwardToError);
		}
		
		this.onSearch = function(_fn){
			callback_Fn = _fn;
		};
	}
	
	function condition(_name, _value, _pattern, _order, _orderSeq){
		
		var name = _name,
			value = _value,
			pattern = _pattern,
			order = _order,
			orderSeq = _orderSeq;
		
		this.getName = function(){return name;}
		
		this.getValue = function(){return value;}
		
		this.setValue = function(_val){value = _val;}
		
		this.getOrder = function(){return order;}
		
		this.setOrder = function(_order){order = _order;}
		
		this.getOrderSeq = function(){return orderSeq;}
		
		this.setOrderSeq = function(_seq){orderSeq = _seq;}
		
		this.getPattern = function(){return pattern;}
		
		this.setPattern = function(_pattern){pattern = _pattern;}
	}


	QSearch.init = function(_url, _type){
		
		var newSearch = new search(_url, _type);
		List[_url] = List.length;				//索引，其实没必要，直接用对象的map性质存储也是可以的，这里为了让代码更好理解
		List.push(newSearch);							//保存创建的对象
		return newSearch;								//初始化完成，返回对象
	}
	
	QSearch.get = function(_url){
		return List[List[_url]];
	}
})(window);